package xyz.labmem.base.sys.module.dict.service.impl

import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import org.springframework.stereotype.Service
import xyz.labmem.base.sys.module.dict.entity.SysDictData
import xyz.labmem.base.sys.module.dict.entity.SysDictType
import xyz.labmem.base.sys.module.dict.mapper.SysDictDataMapper
import xyz.labmem.base.sys.module.dict.mapper.SysDictTypeMapper
import xyz.labmem.base.sys.module.dict.service.SysDictService

/**
 * @Author lty
 * @Date 2021/12/16 16:13
 */
@Service
class SysDictServiceImpl(
    private var sysDictDataMapper: SysDictDataMapper,
    private var sysDictTypeMapper: SysDictTypeMapper,
) : SysDictService {

    override fun getTypeByCode(code: String): SysDictType? {
        return sysDictTypeMapper.selectOne(
            KtQueryWrapper(SysDictType::class.java)
                .eq(SysDictType::code, code)
        )
    }

    override fun getDataListByType(type: SysDictType): List<SysDictData> {
        return sysDictDataMapper.selectList(
            KtQueryWrapper(SysDictData::class.java)
                .eq(SysDictData::typeId, type.id)
                .orderByAsc(SysDictData::sort)
        )
    }

    override fun getDataListByTypeCode(code: String): List<SysDictData> {
        val type = getTypeByCode(code)
        return if (type != null) getDataListByType(type) else ArrayList()
    }


}